package com.tian.controller;

import com.drools.core.KieTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tian.mapper.PatientsMapper;
import com.tian.pojo.Value;

import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


import java.util.Map;


@RestController
@RequestMapping(value = "/")
public class UserController {

    @Autowired
    private KieTemplate kieTemplate;

    @Autowired
    private PatientsMapper userMapper;

    @CrossOrigin
    @RequestMapping(value = "/return", method = RequestMethod.POST)
    @ResponseBody
    public String deleteBook(@RequestBody Map<String,Object> map) throws Exception{


        //    @Autowired
//    private UserMapper userMapper;
//
//    @RequestMapping("/")
//    public List<User> findAll(){
//        return  userMapper.findAll();
//    }

//    public String userMapper(Model model){
//        List<User> users = userMapper.findAll();
//        model.addAttribute("user",users);
//        return "";
//    }




//        net.sf.json.JSONObject mapJson = net.sf.json.JSONObject.fromObject(map);

        Value value = new Value();

        value.setEpidemiologicalHistory(String.valueOf(map.get("Epidemiological-history")));  //流行病学史正确
        value.setListOfSystem((ArrayList<String>)map.get("Symptom"));//复选框
        value.setImagingFeaturesOfCOVID(String.valueOf(map.get("ImagingFeaturesOf-COVID")));  //影像图正确
        value.setTotalLeukocyteCount(Integer.parseInt(String.valueOf(map.get("Total-leukocyte-count"))));  //白细胞计数正确
        value.setLymphocyteCount(Double.valueOf(String.valueOf(map.get("Lymphocyte-count"))));  //淋巴细胞计数正确
        value.setCOVIDVaccine(String.valueOf(map.get("COVID-vaccine")));  //疫苗接种正确


        //drools规则文件
        KieSession Session = kieTemplate.getKieSession("rule.drl");
        Session.insert(value);
        Session.fireAllRules();


        String ROEH = value.getResultOfEpidemiologicalHistory();
        String RS = value.getResultSystem();
        String ROIFOC = value.getResultOfImagingFeaturesOfCOVID();
        String ROTLC = value.getResultOfTotalLeukocyteCount();
        String ROLC = value.getResultOfLymphocyteCount();
        String ROCV = value.getResultOfCOVIDVaccine();


//        ArrayList test = new ArrayList();
//        test.add(a);
//        test.add(b);
//        System.out.println(test);
//        String str=String.join("",test);
//        System.out.println(str);



        //给前端返回的必须是String类型
        ObjectMapper objectMapper = new ObjectMapper();

        return objectMapper.writeValueAsString(value);


    }




//    @CrossOrigin
//    @RequestMapping(value = "/", method = RequestMethod.POST)
//    @ResponseBody
//    public void deleteBookByNumber(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        request.setCharacterEncoding("utf-8");
//        String bookName = request.getParameter("bookName");
//        System.out.println(bookName);
////        return "SUCCESS";
//    }


//    @RequestMapping("/")
//    @ResponseBody
//    public String yourUrl(@RequestParam("username")String username,@RequestParam("password")String password) {
//        System.out.println(username + ";" + password);
//        return "SUCCESS";
//    }

//    @CrossOrigin(origins = "*",maxAge = 3600)
//    @RequestMapping("/")
//    @ResponseBody
//    public Map<String, String> dormsave(@RequestBody Map<String,String> data){
//        System.out.println("save!!!!!!"+data);
//        System.out.println("111111111");
//        return data;
////        Dorm dorm = new Dorm();
////        dorm.setD_id(Integer.parseInt(data.get("d_id")));
////        dorm.setTotal(Integer.parseInt(data.get("total")));
////        dorm.setUsed(Integer.parseInt(data.get("used")));
////        dorm.setDormadmin(data.get("dormadmin"));
//        }


//    @Autowired
//    private UserMapper userMapper;
//
//    @RequestMapping("/")
//    public List<User> findAll(){
//        return  userMapper.findAll();
//    }

//    public String userMapper(Model model){
//        List<User> users = userMapper.findAll();
//        model.addAttribute("user",users);
//        return "";
//    }
}




